package goods

import (
	"context"
	"sale-admin/internal/models"
	"strings"
	"time"

	"192.168.1.75/go-pkg/errorx"
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

type GoodsClassService struct{}

// Set 设置商品标签
func (gc GoodsClassService) Set(ctx context.Context, db *gorm.DB, ClassID, ClassName, ClassDescription, MerchantID string) (err error) {
	if strings.TrimSpace(ClassName) == "" {
		err = errorx.New("标签名称不能为空", -1)
		return
	}
	//新增
	if ClassID == "" {
		//判断重名
		var SameNameExist models.MaGoodsClass
		db.Model(&models.MaGoodsClass{}).
			Where("class_name = ? and deleted = 0", strings.TrimSpace(ClassName)).
			Take(&SameNameExist)
		if SameNameExist.ClassID != "" {
			err = errorx.New("标签名称重复", -1)
			return
		}
		var InsertData models.MaGoodsClass
		InsertData.ClassID = ClassID
		InsertData.MerchantID = MerchantID
		InsertData.ClassName = strings.TrimSpace(ClassName)
		InsertData.ClassDescription = strings.TrimSpace(ClassDescription)
		InsertData.ClassStatus = 0
		InsertData.AddTime = time.Now()
		InsertData.TotalSales = 0
		InsertData.Deleted = 0
		err = db.Model(&models.MaGoodsClass{}).Create(&InsertData).Error
		if err != nil {
			err = errorx.New("新增失败", -1)
			return
		}
	} else { //编辑
		//判断标签名称重名
		var count int64
		db.Model(&models.MaGoodsClass{}).
			Where("class_id != ? and class_name = ? and deleted = 0", ClassID, strings.TrimSpace(ClassName)).
			Count(&count)
		if count != 0 {
			err = errorx.New("标签名称重复", -1)
			return
		}

		//校验标签id是否存在
		var GoodsClassCount int64
		db.Model(&models.MaGoodsClass{}).
			Where("class_id = ? and deleted = 0", ClassID).
			Count(&GoodsClassCount)
		if GoodsClassCount == 0 {
			err = errorx.New("标签信息不存在", -1)
			return
		}
		UpdateErr := db.Model(&models.MaGoodsClass{}).
			Where("class_id = ? and deleted = 0", ClassID).
			Updates(map[string]interface{}{
				"class_name":        strings.TrimSpace(ClassName),
				"class_description": strings.TrimSpace(ClassDescription),
				"add_time":          time.Now(),
			}).Error
		if UpdateErr != nil {
			err = errorx.New("操作失败", -1)
			return
		}
	}
	return
}

// List 获取商品标签列表
func (gc GoodsClassService) List(ctx context.Context, db *gorm.DB, MerchantID string, Page, Limit int) (data interface{}, count int64, err error) {
	GoodsClassList := []models.MaGoodsClass{}
	db.Model(&models.MaGoodsClass{}).Where("merchant_id = ? and deleted = 0", MerchantID).Count(&count)
	helper.Gorm{}.Paginate(db, Page, Limit).Where("merchant_id = ? and deleted = 0", MerchantID).Find(&GoodsClassList)
	for k, v := range GoodsClassList {
		addTimeFormated, _ := models.DateTime(v.AddTime).MarshalDateTime()
		GoodsClassList[k].AddTimeFormated = string(addTimeFormated)
	}
	return GoodsClassList, count, nil
}

// Remove 删除商品标签
func (gc GoodsClassService) Remove(ctx context.Context, db *gorm.DB, MerchantID, ClassID string) (err error) {
	//校验标签id是否存在
	var Count int64
	db.Model(&models.MaGoodsClass{}).
		Where("merchant_id = ? and class_id = ? and deleted = 0", MerchantID, ClassID).
		Count(&Count)
	if Count <= 0 {
		err = errorx.New("标签信息不存在", -1)
		return
	}
	UpdateErr := db.Model(&models.MaGoodsClass{}).
		Where("merchant_id = ? and class_id = ? and deleted = 0", MerchantID, ClassID).
		UpdateColumn("deleted", 1).Error
	if UpdateErr != nil {
		err = errorx.New("操作失败", -1)
		return
	}
	return
}
